import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class ServerSocket implements ISocket {
    private static final String TAG = "SocketServer";
    private static final ServerSocket server = new ServerSocket();

    public static ServerSocket getServer() {
        return server;
    }

    private boolean running = true;
    private DatagramSocket socket;

    public void start() {
        // System.out.println(TAG);
        if (socket == null) {

            try {
                socket = new DatagramSocket(PORT);
                // byte[] buff = new byte[BUFF_SIZE];
                while (running) {
                    DatagramPacket packet = new DatagramPacket(new byte[BUFF_SIZE], BUFF_SIZE);
                    socket.receive(packet);
                    handlerData(packet);

                }
            } catch (SocketException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

    public static void main(String[] args) {
        getServer().start();
    }

    public void stop() {
        running = false;
    }

    private void handlerData(DatagramPacket packet) {

        byte[] data = packet.getData();
//        for (byte b:data){
//            System.out.print("" + b );
//        }
        int what = ISocket.byteArrayToInt(data);//只会计算前四位
        String[] dataStr = new String(data, 4, data.length - 4).split(ISocket.TAG);//减掉int的字节数，int是what开头标志符
        System.out.println("what" + what);
        switch (what) {
            case LOGIN:
                login(packet, dataStr);
                break;
        }
    }

    @Override
    public void login(DatagramPacket packet, String[] datas) {
        if (datas.length>=3){
            String userName = datas[0];
            String password = datas[1];
            String clientE = datas[2];
            String e = ISocket.webEncrypted(userName, password);
            if (e.equals(clientE)) {
                packet.setData(ISocket.intToByteArray(SUCCESS));
                return;
            } else {
                packet.setData(ISocket.intToByteArray(ERROR_ACCESS));
            }
        }else
            packet.setData(ISocket.intToByteArray(ERROR_ARGS));

        new RequestThread(packet).start();
    }

    static class RequestThread extends Thread implements Runnable {
        DatagramPacket packet;

        RequestThread(DatagramPacket packet) {
            this.packet = packet;
        }

        @Override
        public void run() {
            try {
                DatagramSocket client = new DatagramSocket();
                // DatagramPacket rePacket = new DatagramPacket(new byte[BUFF_SIZE], BUFF_SIZE,clientAddress,port);
                client.setSoTimeout(2000);
                client.send(packet);
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
